package com.creative.accountsdk;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.creative.accountsdk.net.LocaleProtocol;
import com.creative.accountsdk.net.Protocol;
import com.creative.accountsdk.net.XHttpHandler;
import com.wbtech.ums.UmsAgent;
import org.json.JSONObject;
import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cz.msebera.android.httpclient.Header;
/**
 * Created by yinjialiang on 15-6-17.
 */
public class AccountSdk {
    private static Application application;
    private static AccountSdk instance;

    public static Application getApplication() {
        return application;
    }

    public static AccountSdk getInstance() {
        if(instance == null)
        {
            instance = new AccountSdk();
        }
        return instance;
    }

    private String appID;
    private String secretKey;
    private OnLoginListener loginListener;
    private int orientation;
    private int dialogWidth;
    private int dialogMarginBottom;

    private AccountSdk(){}

    public void init(Application application, String appID, String secretKey)
    {
        AccountSdk.application = application;
        DataCenter.init(application);

        this.appID = appID;
        this.secretKey = secretKey;

        final LocaleProtocol protocol = new LocaleProtocol();
        DataCenter.getHttpClient().get(protocol.getUrl(), protocol.getParams(), new XHttpHandler(application) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                if (protocol.parse(response)) {
                    String country = protocol.country;
                    if (country.equals("CN")) {
                        DataCenter.LOGIN_LOCAL = DataCenter.LOCAL_CN;
                    } else {
                        DataCenter.LOGIN_LOCAL = DataCenter.LOCAL_EN;
                    }
                }
            }
        });
        UmsAgent.setBaseURL(getUmsBaseUrl());
       // UmsAgent.update(application);
        UmsAgent.postClientData(application);

        UmsAgent.setDefaultReportPolicy(application,0);
        if (Protocol.SERVER == Protocol.SERVER_PUBLIC)
        {
            UmsAgent.onError(application);
        }
        UmsAgent.bindUserIdentifier(application, "0");
        UmsAgent.postTags(application, UmsChannel.getChannel());
    }

    public void login(Context context, OnLoginListener loginListener)
    {

        this.loginListener = loginListener;
        if(UserData.getInstance().exist())
        {
            if(loginListener != null)
            {
                loginListener.onComplete(UserData.getInstance().getJson());
            }
        }
        else {
            Intent intent = new Intent(context, AccountActivity.class);
            context.startActivity(intent);
        }
    }

    public boolean login(Context context,  int requestcode,  OnLoginListener loginListener)
    {

        this.loginListener = loginListener;
        if(UserData.getInstance().exist())
        {
            Log.e("tag onKeyShare", "accountSdk onComplete platform.....name: 1111 UserData.getInstance().exist() " );
            if(loginListener != null)
            {
                loginListener.onComplete(UserData.getInstance().getJson());
            }
            return false;
        }
        else {
            Log.e("tag onKeyShare", "accountSdk onComplete platform.....name: else  22222UserData.getInstance().exist() " );
            Intent intent = new Intent(context, AccountActivity.class);
            // context.startActivity(intent);
            ((Activity)context).startActivityForResult(intent, requestcode);
            return true;
        }
    }

    public void registe(Activity context, OnLoginListener loginListener)
    {

        this.loginListener = loginListener;
        if(UserData.getInstance().exist())
        {
            if(loginListener != null)
            {
                loginListener.onComplete(UserData.getInstance().getJson());
            }
        }
        else {
            Intent intent = new Intent(context, AccountActivity.class);
            intent.putExtra("flag", "registe");
            context.startActivity(intent);
        }
    }

    public void bindWawayaya(Activity context, OnLoginListener loginListener)
    {

        this.loginListener = loginListener;
        Intent intent = new Intent(context, AccountActivity.class);
        intent.putExtra("flag", "bind_wawayaya");
        context.startActivity(intent);
    }

    public void modifyPassword(Activity context, OnLoginListener loginListener)
    {

        this.loginListener = loginListener;
        Intent intent = new Intent(context, AccountActivity.class);
        intent.putExtra("flag", "modify_password");
        context.startActivity(intent);
    }

    public void logout(Context context)
    {
        UserData.getInstance().clear(context);
        UserData.setInstance(null);
        ShareSDK.initSDK(context);
        removeQZone(context);
        removeSinaWeibo(context);
        removeWeChat(context);
        removeFacebook(context);
    }

    public void removeSdk(Context context)
    {
        ShareSDK.initSDK(context);
        removeQZone(context);
        removeSinaWeibo(context);
        removeWeChat(context);
        removeFacebook(context);
    }

     private void  removeQZone(Context  context){
         Platform  qzone=ShareSDK.getPlatform(context,QQ.NAME);
         if(qzone.isValid()){
             qzone.removeAccount();
         }
     }
    private void  removeSinaWeibo(Context  context){
        Platform  qzone=ShareSDK.getPlatform(context,SinaWeibo.NAME);
        if(qzone.isValid()){
            qzone.removeAccount();
        }
    }
    private void  removeWeChat(Context  context){
        Platform  qzone=ShareSDK.getPlatform(context,Wechat.NAME);
        if(qzone.isValid()){
            qzone.removeAccount();
        }
    }
    private void  removeFacebook(Context  context){
        Platform  qzone=ShareSDK.getPlatform(context,Facebook.NAME);
        if(qzone.isValid()){
            qzone.removeAccount();
        }
    }

    public String getAppID() {
        return appID;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public OnLoginListener getLoginListener() {
        return loginListener;
    }

    public int getDialogWidth() {
        return dialogWidth;
    }

    /**
     * 设置对话框的宽度
     * @param dialogWidth
     */
    public void setDialogWidth(int dialogWidth) {
        this.dialogWidth = dialogWidth;
    }

    public int getDialogMarginBottom() {
        return dialogMarginBottom;
    }

    /**
     * 设置对话框的marginBottom
     * @param dialogMarginBottom
     */
    public void setDialogMarginBottom(int dialogMarginBottom) {
        this.dialogMarginBottom = dialogMarginBottom;
    }

    private String getUmsBaseUrl()
    {
        switch (Protocol.SERVER)
        {
            case Protocol.SERVER_TEST:
                return "http://10.135.195.178/index.php?";
            case Protocol.SERVER_PUBLIC_TEST:
                return "http://testapi-joyreader.wawayaya.com:9084/index.php?";
            default:
                return "http://api-joyreader.wawayaya.com:9084/index.php?";
        }
    }

    public static void clearInstance()
    {
        instance = null;
    }

}
